package com.tmeone.erpone.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tmeone.erpone.entity.*;
import com.tmeone.erpone.service.IProductService;
import com.tmeone.erpone.service.ISCompanyService;
import com.tmeone.erpone.service.ITCategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tmeone
 * @since 2021-04-19
 */
@RestController
public class ProductController {

    @Autowired
    private IProductService productService;
    @Autowired
    private ITCategoriesService categoriesService;
    @Autowired
    private ISCompanyService companyService;
    //商品审核分页
    @GetMapping("/productsAudit")
    public JsonResult pageProducts(@RequestParam(value = "curr",required = false,defaultValue = "1") Integer current,
                                   @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize){
            Page<Product> arg = new Page<>(current, pageSize);
            IPage<Product> page = productService.pageProducts(arg);
            return new JsonResult(1,page);
    }
    //商品状态分页
    @GetMapping("/productsManagement")
    public JsonResult pageProductsManagement(@RequestParam(value = "curr",required = false,defaultValue = "1") Integer current,
                                   @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize){
        Page<Product> arg = new Page<>(current, pageSize);
        IPage<Product> page = productService.pageProductsManagement(arg);
        return new JsonResult(1,page);
    }
    //通过名字查询商品（模糊查询）
    @GetMapping("/productName")
    public JsonResult getProductByName(@RequestParam(value = "curr",required = false,defaultValue = "1") Integer current,
                                       @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize,
                                       String productName ){
        Page<Product> arg = new Page<>(current, pageSize);
        IPage<Product> page = productService.findProductByName(arg,productName);
        return new JsonResult(1,page);
    }

    @GetMapping("/productShow")
    public JsonResult getProductById(String id){
        return new JsonResult(1,productService.findProductById(id));
    }
    //审核状态
    @GetMapping("/productAudit")
    public JsonResult getProductAudit(){
        Audit audit = new Audit();
        audit.setAuditNo(String.valueOf(productService.count(new QueryWrapper<Product>().lambda().eq(Product::getaStatus,"0"))));
        audit.setAuditYes(String.valueOf(productService.count(new QueryWrapper<Product>().lambda().eq(Product::getaStatus,"1"))));
        audit.setAuditUn(String.valueOf(productService.count(new QueryWrapper<Product>().lambda().eq(Product::getaStatus,"-1"))));
        return new JsonResult(1,audit);
    }
    //审核通过
    @PutMapping("/auditSuccess")
    public JsonResult auditSuccess(@RequestBody Product product){
        product.setaStatus("1");
        productService.updateById(product);
        return new JsonResult(1,"ok");
    }
    //商品上下架
    @PutMapping("/productManagementStatus")
    public JsonResult productManagementStatus(@RequestBody Product product){
        if (product.getpStatus().equals("1")){
            product.setpStatus("-1");
        }else {
            product.setpStatus("1");
        }
        productService.updateById(product);
        return new JsonResult(1,"ok");
    }
    //审核不通过
    @PutMapping("/auditDanger")
    public JsonResult auditDanger(@RequestBody Product product){
        product.setaStatus("-1");
        productService.updateById(product);
        return new JsonResult(1,"ok");
    }

    @PutMapping("modifyProduct")
    public JsonResult modifyProduct(@RequestBody Product product){
        if (product.getCompanyId()!=""&&product.getCompanyId()!=null){
            product.setCompanyId(companyService.getOne(new QueryWrapper<SCompany>().lambda().eq(SCompany::getCompanyName,product.getCompanyId())).getId());
        }
        if (product.getpEnterprise()!=""&&product.getpEnterprise()!=null){
            product.setpEnterprise(categoriesService.getOne(new QueryWrapper<TCategories>().lambda().eq(TCategories::getName,product.getpEnterprise())).getId());
        }
        productService.updateById(product);
        return new JsonResult(1,"ok");
    }

    @PostMapping("addProduct")
    public JsonResult addProduct(@RequestBody Product product){
        if (product.getCompanyId()!=""&&product.getCompanyId()!=null){
            product.setCompanyId(companyService.getOne(new QueryWrapper<SCompany>().lambda().eq(SCompany::getCompanyName,product.getCompanyId())).getId());
        }
        if (product.getpEnterprise()!=""&&product.getpEnterprise()!=null){
            product.setpEnterprise(categoriesService.getOne(new QueryWrapper<TCategories>().lambda().eq(TCategories::getName,product.getpEnterprise())).getId());
        }
        product.setaStatus("0");
        product.setpStatus("-1");
        productService.save(product);
        return new JsonResult(1,"ok");
    }
}
